package com.atguigu.gulimall.order.service.impl;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.gulimall.order.dao.OrderItemDao;
import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.entity.OrderItemEntity;
import com.atguigu.gulimall.order.entity.OrderReturnReasonEntity;
import com.atguigu.gulimall.order.service.OrderItemService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

@RabbitListener(queues = {"hello-java-queue"})
@Service("orderItemService")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * queues:声明要监听的队列
     * 参数可以写类型
     * @param message 原生消息详细信息 头+体
     * @param content 发送消息的类型
     * @param channel 当前传输数的通道
     *
     * Queue：可以被很多人来监听，只要收到消息，队列删除消息，而且只能有一个收到此消息
     * 场景：
     *     1）、订单服务器启动多个，同一个消息只能被一个客户端接收
     *     2）、只有消息完全处理完，方法运行节后，我们就可以接收到下一个消息
     *
     */
//    @RabbitListener(queues = {"hello-java-queue"})
    @RabbitHandler
    public void recieveMassage(Message message, OrderReturnReasonEntity content, Channel channel) {
        byte[] body = message.getBody();
        MessageProperties properties = message.getMessageProperties();
        System.out.println("接收到消息...："+content);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();

        try {
            //签收货物，非批量模式
            if(deliveryTag % 2 ==0) {
                channel.basicAck(deliveryTag, false);
            } else {
                //long deliveryTag, boolean multiple, boolean requeue
                //requeue==false  丢弃；  requeue=true  重新发回队列
                channel.basicNack(deliveryTag, false, true);
                //long deliveryTag, boolean requeue
//                channel.basicReject(deliveryTag, true);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RabbitHandler
    public void recieveMassage1(OrderEntity content) {
        System.out.println("接收到的消息："+content);
    }

//    @RabbitHandler
//    public void recieveMassage2(OrderItemEntity content) {
//        System.out.println("接收到的消息："+content);
//    }
}